package com.english.controller;

import com.english.entity.CourseSchedule;
import com.english.service.CourseScheduleService;
import com.english.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/course/")
public class CourseScheduleController {

    @Autowired
    private CourseScheduleService courseScheduleService;

    /**
     * 添加课程表
     * @param date 前端传入的日期
     * @return 操作结果
     */
    @PostMapping("/add")
    public R<String> addCourseSchedule(@RequestParam @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss") Date date, @RequestParam String imageUrl, @RequestParam Long classId) {
        try {
            String result = courseScheduleService.addCourseSchedule(date, imageUrl, classId);
            return R.success(result);
        } catch (Exception e) {
            return R.error("日期格式不正确，请使用yyyy-MM-dd'T'HH:mm:ss格式，例如：2025-08-26T08:00:00");
        }
    }

    /**
     * 获取所有课程安排
     * @return 所有课程安排列表
     */
    @GetMapping("/all")
    public R<List<CourseSchedule>> getAllCourseSchedules() {
        try {
            List<CourseSchedule> courseSchedules = courseScheduleService.getAllCourseSchedules();
            return R.success(courseSchedules);
        } catch (Exception e) {
            return R.error("获取课程安排失败：" + e.getMessage());
        }
    }

    /**
     * 根据年月周查询课程表
     * @param date 日期
     * @return 课程表对象
     */
    @GetMapping("/query")
    public R<List<CourseSchedule>> getCourseScheduleByDate(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") Date date) {
        List<CourseSchedule> courseSchedules = courseScheduleService.getCourseScheduleByDate(date);
        return R.success(courseSchedules);
    }

    /**
     * 根据班级ID查询课程表
     * @param classId 班级ID
     * @return 课程表对象列表
     */
    @GetMapping("/queryByClassId")
    public R<List<CourseSchedule>> getCourseScheduleByClassId(@RequestParam Long classId) {
        List<CourseSchedule> courseSchedules = courseScheduleService.getCourseScheduleByClassId(classId);
        return R.success(courseSchedules);
    }

    /**
     * 根据ID修改课程表
     * @param courseSchedule 课程表对象
     * @return 操作结果
     */
    @PutMapping("/update")
    public R<String> updateCourseSchedule(@RequestBody CourseSchedule courseSchedule) {
        String result = courseScheduleService.updateCourseScheduleById(courseSchedule);
        return R.success(result);
    }

    /**
     * 根据ID删除课程表
     * @param id 课程表ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public R<String> deleteCourseScheduleById(@PathVariable Long id) {
        String result = courseScheduleService.deleteCourseScheduleById(id);
        return R.success(result);
    }
}
